package aam.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import aam.model.SubBoardListVO;
import aam.model.board.BoardVO;
import aam.model.member.MemberVO;
import aam.model.subboard.SubBoardVO;
import aam.service.MemberService;
import aam.service.SubBoardService;

@Controller
public class SubBoardController {
	@Resource(name="subBoardServiceImpl")
	private SubBoardService subBoardService;
	@Resource(name="memberServiceImpl")
	private MemberService memberService;
	
	@RequestMapping("/guestbook_board.do")
	public ModelAndView getAllGuestBookList(String pageNo) throws Exception{
		SubBoardListVO list=this.subBoardService.getAllGuestBookList(pageNo);
		return new ModelAndView("guestbook_view.subboard","slist",list);
	}
	
	@RequestMapping("/writeGuestBook.do")
	public String writeGuestBook(HttpSession session,String content) throws Exception{
		MemberVO vo=(MemberVO) session.getAttribute("vo");
		this.subBoardService.writeGuestBook(vo.getId(), content);
		return "redirect:/guestbook_board.do";
	}
	
	@RequestMapping("/deleteGuestBook.do")
	public String deleteGuestBook(String no) throws Exception{
		this.subBoardService.deleteGuestBook(no);
		return "redirect:/guestbook_board.do";
	}
	
	@RequestMapping("/writeReply_subBoard.do")
	public ModelAndView writeReply(HttpSession session,SubBoardVO svo,BoardVO bvo,HttpServletRequest request) throws Exception{
		MemberVO mvo=(MemberVO)session.getAttribute("vo");
		int boardNo=Integer.parseInt(request.getParameter("boardNo"));
		String page=request.getParameter("page");
		svo.setMemberVO(mvo);
		svo.setBoardVO(bvo);
		subBoardService.writeReply(svo);	
		session.setAttribute("vo", memberService.login(mvo));
		return new ModelAndView("redirect:getContentNo_board.do?no="+bvo.getNo()+"&boardNo="+boardNo+"&page="+page);
	}
	
	@RequestMapping("/deleteReply_board.do")
	public ModelAndView deleteReply(HttpServletRequest request,int replyNo,BoardVO bvo) throws Exception{
		subBoardService.deleteReply(replyNo);
		int boardNo=Integer.parseInt(request.getParameter("boardNo"));
		String page=request.getParameter("page");
		return new ModelAndView("redirect:getContentNo_board.do?no="+bvo.getNo()+"&boardNo="+boardNo+"&page="+page);
		
	}
	
	
	
	
}
